package com.itheima.app.controller;


import com.itheima.app.exception.BusinessException;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UsersManager;
import com.itheima.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UsersManager usersManager;

    /**
     * TODO:输入新手机号，发送验证码
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        String mobile = UserHolder.get().getMobile();
        usersManager.sendMsg(mobile);
        return ResponseEntity.ok(null); //正常返回状态码200
    }


    /**
     * TODO:判断验证码是否正确
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        //1、调用map集合获取请求参数
        String mobile = UserHolder.get().getMobile();
        String code = (String) map.get("verificationCode");
        //2、调用userService完成验证码校验
        Boolean aBoolean = usersManager.checkVerificationCode(mobile, code);
        //3、构造返回
        if (!aBoolean){
            throw new BusinessException(ErrorResult.loginError());
        }
        Map<String,Object> map1 = new HashMap();
        map1.put("verification",aBoolean);
        return ResponseEntity.ok(map1);
    }

    /**
     * TODO:跟新用户新
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity updateVerificationCode(@RequestBody Map map){
        //1、调用map集合获取请求参数
        String phone = (String) map.get("phone");
        String code = (String) map.get("verificationCode");
        //2、调用userService完成用户新手机号的保存
        usersManager.updateVerificationInfo(phone);
        //3、构造返回
        return ResponseEntity.ok(null);
    }









}